﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using PlugNT.Common;
using PlugNT.Custom;
using PlugNT.Cms.Core;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Tables
{
    public partial class EditTable : AdminPage
    {
        private Tables curTables = new Tables();
        private Channel curChannel = new Channel();
        private string sid = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            string type = (Request.QueryString["t"] == null) ? "" : Request.QueryString["t"].Trim();
            sid = (Request.QueryString["id"] == null) ? "0" : Request.QueryString["id"].Trim();

            if (!PlugNT.Custom.WebConfig.IsAccess)
            {
                if (!IsPostBack)
                {
                    InitData();
                    switch (type)
                    {
                        case "add":
                            Button_update.Text = " 添加 ";
                            break;
                        case "edit":
                            Button_update.Text = " 更新 ";
                            txtTablesPostfix.BackColor = System.Drawing.Color.Gray;
                            txtTablesPostfix.Enabled = false;
                            break;
                        case "current":
                            SetCurrent();
                            break;
                        case "delete":
                            Delete();
                            break;
                        default:
                            Response.Redirect("../Message.aspx?message=分表绑定失败，请从正确路径入口！" + "&z=no");
                            break;
                    }
                    hidAction.Value = type;
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=分表绑定失败，Access数据库无此项功能！" + "&z=no");
            }
        }

        private void InitData()
        {
            DataTable dt = curChannel.GetOrderList();
            this.ddlBindclslist.Items.Clear();
            this.ddlBindclslist.Items.Add(new ListItem("绑定所有栏目", ""));
            foreach (DataRow dr in dt.Rows)
            {
                int ClassTj = Convert.ToInt32(dr["clslistlen"]);
                string clslist = dr["clslist"].ToString().Trim();
                string ClassName = dr["clsname"].ToString().Trim();

                //==================================
                ListItem li = new ListItem();
                if (ClassTj != 1)
                {
                    ClassName = "├ " + ClassName;
                    ClassName = PlugNT.Common.Utils.StringOfChar(ClassTj - 1, "　") + ClassName;
                }
                li.Text = ClassName;
                li.Value = clslist;
                this.ddlBindclslist.Items.Add(li);
                //==================================
            }

            if (sid != "")
            {
                TablesInfo tables = curTables.GetTablesInfo(int.Parse(sid));
                if (tables != null)
                {
                    this.txtTablesPostfix.Text = tables.postfix.ToString();
                    this.txtCmsPath.Text = tables.dicpath;
                    this.txtTablesDescription.Text = tables.description;
                    this.ddlBindclslist.SelectedValue = tables.bindclslist;
                    this.cbxComment.Checked = tables.crecomment;
                    if (tables.postfix == "")
                    {
                        this.txtTablesDescription.ReadOnly = true;
                    }
                }
            }
        }

        private void SetCurrent()
        {
            TablesInfo tables = curTables.GetTablesInfo(int.Parse(sid));
            if (tables != null)
            {
                NameValue tablepostfix = new NameValue("/configs/db/tablepostfix", tables.postfix.ToString());
                if (WebSeting.Update(tablepostfix) == "")
                {
                    //设置生成静态页文件的路径
                    //PlugNT.Cms.Core.WebPage.PageBuilder.SetCmsPath(WebConfig.CmsPath);
                    Response.Redirect("Admin.aspx");
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=分表设置失败！" + "&z=no");
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=分表设置失败，该表可能已删除！" + "&z=no");
            }
        }

        private void Delete()
        {
            TablesInfo tables = curTables.GetTablesInfo(int.Parse(sid));
            if (tables != null)
            {
                List<TableType> types = new List<TableType>();
                types.Add(TableType.Article);
                if (tables.crecomment)
                {
                    types.Add(TableType.Comment);
                }

                if (PlugNT.Cms.Core.Table.DeleteTable(tables.postfix, types))
                {
                    if (curTables.Delete(int.Parse(sid)))
                    {
                        Response.Redirect("Admin.aspx");
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=分表删除--失败！" + "&z=no");
                    }
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=分表删除错误！" + "&z=no");
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=分表删除失败，非法操作！" + "&z=no");
            }
        }


        protected void Button_update_Click(object sender, EventArgs e)
        {
            string  postfix =txtTablesPostfix.Text.Trim();
            string dicpath = txtCmsPath.Text.Trim();
            string description = txtTablesDescription.Text.Trim();
            string bindclslist = this.ddlBindclslist.SelectedValue;
            bool crecomment = this.cbxComment.Checked;

            if (description != "")
            {
                if (hidAction.Value == "add")
                {
                    TablesInfo tables = new TablesInfo();
                    if (!curTables.Exists(postfix))
                    {
                        List<TableType> types = new List<TableType>();
                        types.Add(TableType.Article);
                        if (crecomment)
                        {
                            types.Add(TableType.Comment);
                        }

                        if (PlugNT.Cms.Core.Table.CreateTable(postfix, types))
                        {
                            tables.postfix = postfix;
                            tables.dicpath = dicpath;
                            tables.description = description;
                            tables.bindclslist = bindclslist;
                            tables.crecomment = crecomment;

                            if (curTables.Add(tables))
                            {
                                if (PlugNT.Custom.WebConfig.TablePostfix == tables.postfix)
                                {
                                    SetCurrent();
                                }
                                else
                                {
                                    Response.Redirect("Admin.aspx");
                                }
                            }
                            else
                            {
                                Response.Redirect("../Message.aspx?message=添加分表失败，分表创建成功！&z=no");
                            }
                        }
                        else
                        {
                            Response.Redirect("../Message.aspx?message=添加分表失败，分表创建错误！&z=no");
                        }
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=添加分表失败，分表已创建！&z=no");
                    }
                }
                else
                {
                    if (hidAction.Value == "edit")
                    {
                        TablesInfo tables = curTables.GetTablesInfo(int.Parse(sid));
                        if (tables != null)
                        {
                            tables.dicpath = dicpath;
                            tables.description = description;
                            tables.bindclslist = bindclslist;

                            bool commentflag = true;
                            if (tables.crecomment != crecomment)
                            {
                                List<TableType> types = new List<TableType>();
                                types.Add(TableType.Comment);
                                if (crecomment)
                                {
                                    commentflag = PlugNT.Cms.Core.Table.CreateTable(postfix, types);
                                }
                                else
                                {
                                    commentflag = PlugNT.Cms.Core.Table.DeleteTable(postfix, types);
                                }
                            }

                            if (commentflag)
                            {
                                if (curTables.Update(tables))
                                {
                                    if (PlugNT.Custom.WebConfig.TablePostfix == tables.postfix)
                                    {
                                        SetCurrent();
                                    }
                                    else
                                    {
                                        Response.Redirect("Admin.aspx");
                                    }
                                }
                                else
                                {
                                    Response.Redirect("../Message.aspx?message=修改分表失败！" + "&z=no");
                                }
                            }
                            else
                            {
                                Response.Redirect("../Message.aspx?message=删除或修改分表失败！" + "&z=no");
                            }
                        }
                        else
                        {
                            Response.Redirect("../Message.aspx?message=修改分表失败，分表描述为空！" + "&z=no");
                        }
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=分表修改失败，未知操作！" + "&z=no");
                    }
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=分表修改失败，分表描述空！" + "&z=no");
            }
        }

    }
}
